package com.recruit.g1.company.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.recruit.g1.pojo.job.vo.JobVO;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@MappedTypes(List.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
@Component
public class JsonToJobListTypeHandler extends BaseTypeHandler<List<JobVO>> {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public List<JobVO> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String json = rs.getString(columnName);
        try {
            return objectMapper.readValue(json, new TypeReference<List<JobVO>>() {});
        } catch (Exception e) {
            throw new RuntimeException("JSON 解析失败: " + json, e);
        }
    }

    // 其他方法（未使用，但需实现）
    @Override public void setNonNullParameter(PreparedStatement ps, int i, List<JobVO> parameter, JdbcType jdbcType) {}
    @Override public List<JobVO> getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return null; }
    @Override public List<JobVO> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return null; }
}